<?php
header('Content-Type: text/html; charset=UTF-8');
if(!defined('INC_PATH')){
	define("INC_PATH","../../../");
}
require_once(INC_PATH."lib_new/db.php");
$member_id = $_COOKIE['sav_memberid'];
$con = con_open();
$sql_select = "
SELECT * 
FROM smd_takarakuji_janken
WHERE del_flag = 0
";
$res_select = pg_query($con, $sql_select);
$win_probability = 0;
$lose_probability = 0;
$win_probability = pg_fetch_result($res_select, "win");
$lose_probability = pg_fetch_result($res_select, "lose");
$janken_user = $_REQUEST["janken_user"];

$num = rand(0,99);
$janken_doruken = $janken_user;
$janken_result = 2;
if($num < $lose_probability){
	$janken_result = 1;
	if($janken_user == 0){
		$janken_doruken = 2;
	}else if($janken_user == 1){
		$janken_doruken = 0;
	}else if($janken_user == 2){
		$janken_doruken = 1;
	}
}else if($num < $win_probability + $lose_probability){
	$janken_result = 0;
	if($janken_user == 0){
		$janken_doruken = 1;
	}else if($janken_user == 1){
		$janken_doruken = 2;
	}else if($janken_user == 2){
		$janken_doruken = 0;
	}
}

if($janken_result == 0){
	
	
	$sql_self = "
	SELECT fukubiki FROM member
	WHERE memberid = ".$member_id." ";
	$res_self = pg_query($con, $sql_self);
	$ticket_count = pg_fetch_result($res_self, "fukubiki");

	$sql_self_null = "
		SELECT count(0) AS cnt FROM smd_takarakuji_get
		WHERE member_id = ".$member_id."
		AND prize = 0
		AND flag = 0
		AND date_trunc('month',regist_date) = date_trunc('month',now() + '-1 months')
	";
	$res_self_null = pg_query($con, $sql_self_null);
	$null_count = pg_fetch_result($res_self_null, "cnt");
	$add_value = floor($null_count/10);
	$sql_up = "UPDATE member SET fukubiki = (fukubiki + ".$add_value.")
		WHERE memberid = $member_id ";
	$res_up = pg_query($con,$sql_up);
	
	$file_log = fopen("./log/janken_log.txt","a");
	fwrite($file_log,date ("Y-m-d H:i:s",time())."\n".$member_id."\n".$sql_self_null."\n".$sql_up."\n".$res_up."\n");
	fclose($file_log);
	
	
	//ドル箱宝くじ交換券プレゼント
	$sql_getpoint2 = "
		INSERT INTO getpoint (
		memberid
		,itemid
		,point
		,cause
		,comment
		,clickdate
		,actiondate
		,fixdate
		,fixflg
		,payment
		,registdate
		,gamepoint
		) VALUES (
		$member_id
		,-6
		,0
		,'ドル箱宝くじ交換券プレゼント'
		,'takarakuji janken'
		,now()
		,now()
		,now()
		,1
		,$add_value
		,now()
		,0
		)
		";
	$sql_getpoint2 = mb_convert_encoding($sql_getpoint2,"EUC-JP","UTF-8");
	$res_setpoint2 = pg_query($con,$sql_getpoint2);
	
}
if($janken_result != 2){
	$sql_add = "UPDATE smd_takarakuji_get 
		SET flag=1 
		WHERE member_id=".$member_id."
		AND prize = 0
		AND date_trunc('month',regist_date) = date_trunc('month',now() + '-1 month')";
		$res_add = pg_query($con, $sql_add);
}
con_close($con);
echo '<data>
	<comment>じゃんけん結果</comment>
	<setVariable type="text" name="janken_user" value="'.getJankenText($janken_user).'" />
	<setVariable type="text" name="janken_doruken" value="'.getJankenText($janken_doruken).'" />
	<setVariable type="text" name="janken_result" value="'.$janken_result.'" />';
if($janken_result == 0){
	$_SESSION["login"]["fukubiki"] = $_SESSION["login"]["fukubiki"] + $add_value;
	echo '<setVariable type="text" name="ticket_count" value="'.($ticket_count + $add_value).'" />';
}
echo '<setVariable type="text" name="null_count" value="0" />
		<setJankenResult janken_doruken="'.$janken_doruken.'" janken_result="'.$janken_result.'"/>
		<phpover />
	</data>';
function getJankenText($value){
	if($value == 0){
		return "ｸﾞｰ";
	}else if($value == 1){
		return "ﾁｮｷ";
	}else{
		return "ﾊﾟｰ";
	}
}
?>